package xyz.xpman.prisonerfitness.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import xyz.xpman.prisonerfitness.common.App;

@SuppressWarnings("unused")
public class FileUtil {


    private FileUtil() {}



    public static File getCacheDir(@NotNull Context context,
                                   @NotNull String uniqueDirName) {

        final String cacheDir = context.getCacheDir().getPath();

        return new File(cacheDir + File.separator + uniqueDirName);
    }


    /**
     * 获取DiskLruCache的缓存文件夹 注意第二个参数dataType
     * DiskLruCache用一个String类型的唯一值对不同类型的数据进行区分.
     * 比如bitmap,object等文件夹.其中在bitmap文件夹中缓存了图片. card/Android/data/<application
     * package>/cache 如果 缓存数据的存放位置为: /sdSD卡不存在时缓存存放位置为: /data/data/<application
     * package>/cache
     */
    public static File getAvailableCacheDir(@NotNull Context context,
                                            @NotNull String uniqueDirName) {
        String dirPath;
        if (Environment.MEDIA_MOUNTED
                .equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            dirPath = context.getExternalCacheDir().getPath();
        }else{
            dirPath = context.getCacheDir().getPath();
        }
        return new File(dirPath + File.separator + uniqueDirName);
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


/*    public static File getInternalFile(String fileName) {
        // private static String PACKAGE_NAME = "";
        // data/data/cn.edu.jxust.online/info.txt
        final String absoluteFileName = "data/data/" + PACKAGE_NAME + fileName;
        return new File(absoluteFileName);
    }*/


    public static boolean hasExternalStorage() {
        return  Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES).exists();
    }

    public static File getExternalFile(String fileName) {
        File espDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);

        return new File(espDir, fileName);
    }

    public static String externalFile(String fileName) {
        String espDir = Environment.getExternalStorageDirectory().toString();

        return espDir + File.separator + fileName;
    }




    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public static String getFilesDirPath() {
        return App.getContext().getFilesDir().getAbsolutePath();
    }

    public static String getFilesDirPath(String fileName) {
        return getFilesDirPath() + File.separator + fileName;
    }


    public static File getFilesDir() {
        return new File(getFilesDirPath());
    }


    public static File getFilesDir(String fileName) {
        return new File(getFilesDirPath(fileName));
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    public static boolean saveToFile(Context context, String fileName, String data){
        boolean success = true;

        try (OutputStream out = context.openFileOutput(fileName , Context.MODE_PRIVATE);
             OutputStreamWriter outWriter = new OutputStreamWriter(out)) {

            outWriter.write(data);
        } catch (IOException e) {
            success = false;

            e.printStackTrace();
        }
        return success;
    }




    public static boolean saveToFile(String fileName, String data){
        boolean success = true;

        try (OutputStream out = new FileOutputStream(fileName);
             OutputStreamWriter outWriter = new OutputStreamWriter(out)) {

            outWriter.write(data);
        } catch (IOException e) {
            success = false;

            e.printStackTrace();
        }
        return success;
    }

    public static boolean saveToFile(@NotNull String fileName, @NotNull Bitmap data){
        final File file = new File(fileName);

        if (!file.exists() && !createFile(file)) {
            return false;
        }

        boolean success = true;

        try (OutputStream out = new FileOutputStream(fileName)) {
            data.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
        } catch (IOException e) {
            success = false;

            e.printStackTrace();
        }
        return success;
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public static boolean isFileExists(String fileName) {
        if (StrKit.isBlank(fileName)) {
            return false;
        }

        File file = new File(fileName);

        return file.exists();
    }

    public static String read(String fileName) {
        String data = null;

        if (isFileExists(fileName)) {
            try (FileInputStream fileInputStream = new FileInputStream(fileName)) {

                data = StreamUtils.read(fileInputStream);

            } catch (IOException e) {
                data = "";
                e.printStackTrace();
            }
        }
        return data;
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    private static void mkdir(@NotNull File dir) {
        if (dir.exists()) {
            return;
        }

        final File parent = dir.getParentFile();

        if (null == parent || parent.exists()) {
            mkdir(dir);
            return;
        }
        mkdir(parent);
    }


    public static boolean createFile(@NotNull File file) {
        if (file.exists()) {
            return file.canWrite();
        }


        final File dir = file.getParentFile();

        if (null != dir && !dir.exists() && !dir.mkdirs()) {
            return false;
        }


        boolean success = false;

        try {
            success = file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success;
    }


    public static String getSimpleFileName(String fileName) {
        if (StrKit.notBlank(fileName)) {
            int index = fileName.lastIndexOf('.');
            int separatorIndex = fileName.lastIndexOf(File.separator);

            if (-1 != separatorIndex && index > separatorIndex
                    && index != fileName.length()) {
                return fileName.substring(separatorIndex + 1, index);
            }
        }
        return "";
    }

    public static boolean deleteIfExists(@NotNull File file) {
        return !file.exists() || file.delete();
    }

    public static boolean mkdirs(@NotNull File file) {
        return file.exists() || file.mkdirs();
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -




}
